package com.example;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

/**
 * @Author 郅占伟
 * @ClassName SendEmail
 * @Description TODO
 * @Date 2023年12月22日
 * @Version: 1.0
 */
public class SendEmail {

    public static void main(String[] args) throws MessagingException {
        Properties prop = new Properties();
        prop.put("mail.host", "smtp.163.com");
        prop.put("mail.transport.protocol", "smtp");
        prop.put("mail.smtp.auth", true);

        //使用java发送邮件5步骤
        //1.创建sesssion
        Session session = Session.getInstance(prop);
        //开启session的调试模式，可以查看当前邮件发送状态
        session.setDebug(true);

        //2.通过session获取Transport对象（发送邮件的核心API）
        Transport ts = session.getTransport();
        //3.通过邮件用户名密码链接
        ts.connect("13633752048@163.com", "PQRLGZHDWXATPYWZ");

        //4.创建邮件
        Message msg = createSimpleMail(session);

        //5.发送电子邮件
        ts.sendMessage(msg, msg.getAllRecipients());
    }

    public static MimeMessage createSimpleMail(Session session) throws AddressException, MessagingException {
        //创建邮件对象
        MimeMessage mm = new MimeMessage(session);
        //设置发件人
        mm.setFrom(new InternetAddress("13633752048@163.com"));
        //设置收件人
        mm.setRecipient(Message.RecipientType.TO, new InternetAddress("2303206517@qq.com"));
        //设置抄送人
        //mm.setRecipient(Message.RecipientType.CC, new InternetAddress(""));

        mm.setSubject("第一封JAVA邮件！");
        mm.setContent("你好，这是我发送的第一封邮件，异常信息：select * from user", "text/html;charset=utf-8");

        return mm;
    }


}
